package com.hl.qzzpbackend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.hl.qzzpbackend.exception.BusinessException;
import com.hl.qzzpbackend.exception.ErrorCode;
import com.hl.qzzpbackend.manager.upload.FileLogoUpload;
import com.hl.qzzpbackend.manager.upload.FileResumeUpload;
import com.hl.qzzpbackend.model.dto.file.ResumeUploadResult;
import com.hl.qzzpbackend.model.entity.Resume;
import com.hl.qzzpbackend.model.entity.User;
import com.hl.qzzpbackend.service.FileService;
import com.hl.qzzpbackend.service.ResumeService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 文件业务层接口
 *
 * @author helong
 */
@Service
public class FileServiceImpl implements FileService {

    @Resource
    private FileLogoUpload fileLogoUpload;

    @Resource
    private FileResumeUpload fileResumeUpload;

    @Resource
    private ResumeService resumeService;

    /**
     * 上传公司logo
     * @param multipartFile 文件
     * @param loginUser 登录用户
     * @return logo url
     */
    @Override
    public String uploadLogo(MultipartFile multipartFile, User loginUser) {
        String uploadPathPrefix = String.format("logo/%s", loginUser.getId());
        return fileLogoUpload.uploadLogo(multipartFile,uploadPathPrefix);
    }


    /**
     * 上传简历附件
     * @param multipartFile 文件
     * @param loginUser 登录用户
     * @return 是否成功
     */
    @Override
    public boolean uploadResume(MultipartFile multipartFile, User loginUser) {
        // 上传文件
        ResumeUploadResult uploadResumeResult = fileResumeUpload.uploadResume(multipartFile, "resume/" + loginUser.getId());

        // 保存简历附件信息
        Resume resume = new Resume();
        BeanUtil.copyProperties(uploadResumeResult, resume);
        resume.setUserId(loginUser.getId());

        boolean result = resumeService.save(resume);
        if(!result){
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"保存简历附件信息失败");
        }
        return true;
    }
}
